// 格式化时间（根据传入时间，与当前时间相比得出几天前，几小时前，几分钟或者是刚刚）

import { ref } from 'vue'

//时间函数date格式为：xxxx-xx-xx xx:xx
export const formatMessageDate = (date: any) => {
  // 这里的date的字符串是格式xxxx-xx-xx xx:xx，但是也可以传数字 数字就必须要1705812785000
  console.log(date,"时间是多少");

  const newTime = Date.parse(new Date() as any) //获得当前时间，转化时间戳
  console.log(newTime,'现在时间是多少');
 console.log(new Date(date),"化成时间");

  const interval = (newTime - new Date(date).getTime()) / 1000
console.log(interval,"时间差");

  if (interval < 0) {
    return '刚刚'
  } else if (interval > 24 * 3600) {
    return getformatTime(new Date(date))
  } else if (interval > 3600) {
    return Math.round(interval / 3600) + '小时前'
  } else if (interval > 60) {
    return Math.round(interval / 60) + '分钟前'
  } else {
    return '刚刚'
  }
}

// 格式化时间（xxxx年xx月xx日 xx:xx）
/**
 * 获取格式化的时间字符串
 * @param {Date} time - 需要格式化的日期对象
 * @returns {string} - 格式化后的时间字符串，格式为YYYY-MM-DD HH:mm
 */
function getformatTime(time: Date) {
  const year = time.getFullYear() // 获取年份
  const month = time.getMonth() + 1 // 获取月份，因为getMonth()返回的是0-11，所以需要+1
  const day = time.getDate() // 获取日期
  const hour = time.getHours() // 获取小时
  const min = time.getMinutes() // 获取分钟
  return `${year}-${month}-${day} ${hour}:${min}` // 返回格式化后的时间字符串
}

// 获取当前具体时间
export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const min = date.getMinutes()
  return `${year}-${month}-${day} ${hour}:${min < 10 ? '0' + min : min}`
}

// 空间动态时间的函数
export const year = (time: string): string => {
  const date = new Date(time)
  const year = date.getFullYear()
  return `${year}`
}

export const month = (time: string): string => {
  const date = new Date(time)
  const month = String(date.getMonth() + 1).padStart(2, '0')
  return `${month}月`
}

export const day = (time: string): string => {
  const date = new Date(time)
  const day = String(date.getDate()).padStart(2, '0')
  return `${day}`
}

export const hour = (time: string): string => {
  const date = new Date(time)
  const hour = String(date.getHours())
  return `${hour}`
}

export const min = (time: string): string => {
  const date = new Date(time)
  const min = date.getMinutes()
  return `${String(min < 10 ? '0' + min : min)}`
}

// 获取聊天时间 （相差300s内的信息不会显示时间）
export const getChatTime = (v1, v2) => {
  v1 = v1.toString().length < 13 ? v1 * 1000 : v1
  v2 = v2.toString().length < 13 ? v2 * 1000 : v2
  if ((parseInt(v1) - parseInt(v2)) / 1000 > 300) {
    return getFormattedTime(v1)
  }
}

// Formatting function to display the time
export const getFormattedTime = (shorttime) => {
  shorttime = shorttime.toString().length < 13 ? shorttime * 1000 : shorttime
  const date = new Date(parseInt(shorttime))
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  let hour = date.getHours().toString().padStart(2, '0')
  const minute = date.getMinutes().toString().padStart(2, '0')

  let meridiem = '上午'
  if (hour >= 12) {
    meridiem = '下午'
    hour = (hour - 12).toString().padStart(2, '0')
  }
  if (hour === '00') {
    hour = '12'
  }

  return `${year}/${month}/${day} ${meridiem}${hour}:${minute}`
}

// 计算评论关注  时间差
// 时间格式化时间为：刚刚、多少分钟前、多少天前
// stringTime 2020-09-10 20:20:20
export const getDateBeforeNow = (stringTime) => {
  console.log('传参未格式化', stringTime)
  stringTime = new Date(stringTime.replace(/-/g, '/'))

  // 统一单位换算
  const minute = 1000 * 60
  const hour = minute * 60
  const day = hour * 24
  const week = day * 7
  const month = day * 30
  const year = month * 12

  const time1 = new Date().getTime() //当前的时间戳
  console.log('当前时间', time1)

  // 对时间进行毫秒单位转换
  const time2 = new Date(stringTime).getTime() //指定时间的时间戳

  console.log('传过来的时间', time2)

  const time = time1 - time2
  console.log('计算后的时间', time)

  const result = ref<any>(null)
  if (time < 0) {
    // alert("设置的时间不能早于当前时间！");
    result.value = stringTime
  } else if (time / year >= 1) {
    const na = time / year
    result.value = parseInt(toString(na)) + '年前'
  } else if (time / month >= 1) {
    result.value = parseInt(time / month) + '月前'
  } else if (time / week >= 1) {
    result.value = parseInt(time / week) + '周前'
  } else if (time / day >= 1) {
    result.value = parseInt(time / day) + '天前'
  } else if (time / hour >= 1) {
    result.value = parseInt(time / hour) + '小时前'
  } else if (time / minute >= 1) {
    result.value = parseInt(time / minute) + '分钟前'
  } else {
    result.value = '刚刚'
  }
  console.log('格式化后的时间', result)
  return result
}
